/**
 *
 * 项目简介：前端开发库，含常用函数及流行框架扩展。
 * 
 * 依赖关系：
 * qp		jQuery
 * qp.bs	Boostrap
 * qp.eu	EasyUI
 */

/*严格模式*/
'use strict';

/*定义对象*/
var qp = {
	config: {},
	boostrap: {},
	dom: {},
	easyui: {},
	function: {},
	json: {},
};

/*配置参数*/
qp.config = {
	debug: true,
	jsonUrl: ''
};

/*Boostrap框架*/
qp.boostrap = {
	//显示模态窗口
	showModal: function (title, body) {
		var _modal = {
			dialog: $('<div class="modal fade" role="dialog"></div>'),
			document: $('<div class="modal-dialog modal-lg" role="document"></div>'),
			content: $('<div class="modal-content"></div>'),
			header: $('<div class="modal-header"></div>'),
			close: $('<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>'),
			title: $('<h4 class="modal-title"></h4>'),
			body: $('<div class="modal-body"></div>'),
			footer: $('<div class="modal-footer"></div>'),
			ok: $('<button type="button" class="btn btn-primary btn-block btn-lg" data-dismiss="modal">确定</button>')
		};
		_modal.document.appendTo(_modal.dialog);
		_modal.content.appendTo(_modal.document);
		_modal.header.appendTo(_modal.content);
		_modal.close.appendTo(_modal.header);
		_modal.title.html(title).appendTo(_modal.header);
		_modal.body.css({ wordWrap: 'break-word' }).html(body).appendTo(_modal.content);
		_modal.footer.appendTo(_modal.content);
		_modal.ok.appendTo(_modal.footer);
		_modal.dialog.on('hidden.bs.modal', function (e) {
			$(this).remove();
		}).modal({
			backdrop: 'static'
		});
		return _modal;
	},
	showJson: function (dom, data) {
		//todo
		return qp.dom.showJson;
	}
};

/*DOM操作*/
qp.dom = {
	//显示代码
	showJson: function (dom, data) {
		return $('<pre></pre>')
			.appendTo(dom.empty())
			.html(qp.json.format(data));
	}
};

/*EasyUI框架*/
qp.easyui = {};

/*公共函数*/
qp.function = {
	//获取请求参数的值
	get: function (name) {
		var _query = window.location.hash ? window.location.hash : window.location.search;
		var _re = new RegExp('[#|\?|&]' + name + '=(.+?)(&|$)');
		var _m = _query.match(_re);
		return _m ? decodeURI(_m[1]) : null;
	}
};

/*JSON对象*/
qp.json = {
	//获取数据
	get: function (param, callback) {
		$.ajax({
			async: true,
			data: param,
			dataType: 'jsonp',
			jsonp: 'cb',
			type: 'get',
			url: qp.config.jsonUrl,
			error: function (req, txt) {
				callback({ errcode: req.status, errmsg: req.statusText, statustxt: txt });
			},
			success: function (data) {
				callback(data);
			}
		});
	},
	//格式化
	format: function (data) {
		return typeof (data) === 'object' ? JSON.stringify(data, null, 2) : data;
	}
};

qp.cfg = qp.config;
qp.bs = qp.boostrap;
qp.eu = qp.easyui;
qp.fun = qp.function;
qp.jo = qp.json;
